/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package shogi;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.util.*;
import java.io.*;

/**
 *
 * @author andy
 */
public class Options {

    private OptionsMenu optionsProduct = new OptionsMenu();
    private JMenuBar mb;
    private JMenu options;
    private JFrame frame;
    private JMenuItem help;
    private JMenuItem version;
    private JMenuItem credits;
    private JMenuItem save;
    private JMenuItem resume;
    private JMenuItem pause;
    private JMenuItem quit;
    private JMenu shogiPiece;
    private JMenuItem newGame;
    private JMenuItem load;
    private JRadioButtonMenuItem kanji, western;
    private String loadGame;
    private String reset = "lxpxxxPxLnbpxxxPRNsxpxxxPxSgxpxxxPxGkxpxxxPxKgxpxxxPxGsxpxxxPxSnrpxxxPBNlxpxxxPxL000000000000001";
    private String directory = "resources/";

    Options(JFrame shogiframe) {
        frame = shogiframe;
        optionsProduct.setBar(this, frame);
        addListeners();
    }

    public void setGame() {
        shogiPiece = new JMenu("Shogi Piece");
        newGame = new JMenuItem("New Game");
        resume = new JMenuItem("Resume");
        pause = new JMenuItem("Pause", KeyEvent.VK_O);
        kanji = new JRadioButtonMenuItem("漢字", new ImageIcon(getClass().getResource("resources1/kLeft.png")));
        western = new JRadioButtonMenuItem("Western", new ImageIcon(getClass().getResource("resources/kLeft.png")));
        ButtonGroup bg = new ButtonGroup();
        bg.add(western);
        western.isSelected();
        bg.add(kanji);
        western.setSelected(true);
        shogiPiece.add(western);
        shogiPiece.add(kanji);
        optionsProduct.getGame().add(shogiPiece);
        optionsProduct.getGame().addSeparator();
        optionsProduct.getGame().add(pause);
        optionsProduct.getGame().addSeparator();
        optionsProduct.getGame().add(newGame);
    }

    public void setResource() {
        help = new JMenuItem("help");
        version = new JMenuItem("Version");
        credits = new JMenuItem("Credits");
        optionsProduct.getResource().add(help);
        optionsProduct.getResource().add(version);
        optionsProduct.getResource().add(credits);
    }
    ActionListener action = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            AbstractButton rb = (AbstractButton) e.getSource();
            if (rb.equals(version)) {
                String s = "shogi version 1.0";
                JOptionPane.showMessageDialog(null, s);
            }
            credits(rb);
            quit(rb);
            pause(rb);
            kanji(rb);
            western(rb);
            save(rb);
            load(rb);
            newGame(rb);
            help(rb);
        }

        private void help(AbstractButton rb) {
            if (rb.equals(help)) {
                //String wd = getWorkingDir()+;
                try {
                    //if ((new File(wd)).exists()) 
                    //{ 
                    Process p = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + getClass().getResource("Shogi.pdf"));
                    p.waitFor();
                    /* } else {
                     System.out.println("File is not exists");
                     }*/
                    System.out.println("Done");
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }

        private void newGame(AbstractButton rb) {
            if (rb.equals(newGame)) {
                frame.dispose();
                new Shogi(reset, directory);
                // ((ShogiBoard)frame).loadBoard(reset);
            }
        }

        private void load(AbstractButton rb) {
            if (rb.equals(load)) {
                JFileChooser fc = new JFileChooser();
                int returnVal = fc.showOpenDialog(load);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();
                    try {
                        BufferedReader in = new BufferedReader(new FileReader(file.getAbsolutePath()));
                        loadGame = in.readLine();
                        directory = in.readLine();
                        in.close();
                        frame.dispose();
                        new Shogi(loadGame, directory);
                        //((ShogiBoard)frame).loadBoard(loadGame);
                    } catch (Exception x) {
                        JOptionPane.showMessageDialog(null, "error loading!");
                    }
                }
            }
        }

        private void save(AbstractButton rb) {
            if (rb.equals(save)) {
                JFileChooser fc = new JFileChooser();
                char[] s = ((ShogiBoard) frame).getSaveGame();
                String saved = new String(s);
                String d = ShogiBoard.getIconSetDirectory();
                int returnVal = fc.showSaveDialog(save);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();
                    try {
                        BufferedWriter out = new BufferedWriter(new FileWriter(file));
                        out.write(saved);
                        out.write("\n" + d);
                        out.close();
                    } catch (Exception x) {
                        JOptionPane.showMessageDialog(null, "error saving!");
                    }
                }
            }
        }

        private void western(AbstractButton rb) {
            if (rb.equals(western)) {
                directory = "resources/";
                char[] s = ((ShogiBoard) frame).getSaveGame();
                String saved = new String(s);
                directory = "resources/";
                frame.dispose();
                new Shogi(saved, directory);
            }
        }

        private void kanji(AbstractButton rb) {
            if (rb.equals(kanji)) {
                directory = "resources1/";
                char[] s = ((ShogiBoard) frame).getSaveGame();
                String saved = new String(s);
                directory = "resources1/";
                frame.dispose();
                new Shogi(saved, directory);
            }
        }

        private void pause(AbstractButton rb) {
            if (rb.equals(pause)) {
                JOptionPane.showMessageDialog(null, "COFFEE BREAK!!");
            }
        }

        private void quit(AbstractButton rb) {
            if (rb.equals(quit)) {
                exit();
            }
        }

        private void credits(AbstractButton rb) {
            if (rb.equals(credits)) {
                String s = "Team Members:"
                        + "\nNova"
                        + "\nCody"
                        + "\nAndy";
                JOptionPane.showMessageDialog(null, s);
            }
        }
    };

    void addListeners() {
        version.addActionListener(action);
        credits.addActionListener(action);
        quit.addActionListener(action);
        pause.addActionListener(action);
        western.addActionListener(action);
        kanji.addActionListener(action);
        save.addActionListener(action);
        load.addActionListener(action);
        newGame.addActionListener(action);
        help.addActionListener(action);
    }

    void exit() {
        System.exit(0);
    }

    String getWorkingDir() {
        File dir1 = new File(".");
        File dir2 = new File("..");
        String current = null, parent = null;
        try {
            current = dir1.getCanonicalPath();
            parent = dir2.getCanonicalPath();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return current;
    }

    public void setMb(JMenuBar mb) {
        this.mb = mb;
    }

    public JMenuBar getMb() {
        return mb;
    }

    public void setSave(JMenuItem save) {
        this.save = save;
    }

    public void setQuit(JMenuItem quit) {
        this.quit = quit;
    }

    public void setLoad(JMenuItem load) {
        this.load = load;
    }

    public JMenuItem getSave() {
        return save;
    }

    public JMenuItem getLoad() {
        return load;
    }

    public JMenuItem getQuit() {
        return quit;
    }
}
